package com.example.weather_plus.ui.weathergram.view;

import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.weather_plus.R;
import com.example.weather_plus.databinding.ActivityWeathergramBinding;

public class DiaryActivity extends AppCompatActivity {

    ActivityWeathergramBinding binding;
    EntriesFragment entries;
    //    DiaryFragment diary;
    Fragment current;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityWeathergramBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initMethod();

        // 底部菜单栏按钮
        ImageButton add_button = binding.add;
        ImageButton menu = binding.button1;         // TODO:设置菜单视图
        ImageButton photo = binding.button3;   // TODO:设置添加照片功能


//        add_button.setOnClickListener(v -> {
//            changeFragment(current, diary);
//        });

        menu.setOnClickListener(v -> {
            Toast.makeText(this, "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });

        photo.setOnClickListener(v -> {
            Toast.makeText(this, "该功能正在建设中", Toast.LENGTH_SHORT).show();
        });


    }


    // 用于初始化activity的内部方法
    private void initMethod() {
        entries = new EntriesFragment(this);
//        calender = new CalenderFragment();
//        diary = new DiaryFragment();

        changeFragment(null, entries);
        current = entries;

        binding.bottomNavigation.setOnItemSelectedListener(item -> {

            int itemId = item.getItemId();

            if (itemId == R.id.menu_entries) {
                // 每次切换到entries时刷新视图
                if (current != null)
                    entries = new EntriesFragment(this);
                changeFragment(current, entries);
            } else {
                Toast.makeText(this, "该功能正在建设中", Toast.LENGTH_SHORT).show();
            }
//            else if (itemId == R.id.menu_calender) {
//                changeFragment(current, calender);
//            } else if (itemId == R.id.menu_diary) {
//                changeFragment(current, diary);
//            } else {
//                // TODO 报错
//            }

            item.setChecked(true);

            return false;
        });
    }

    public void flash() {
        entries = new EntriesFragment(this);
        changeFragment(current, entries);
    }

    // fragment切换
    public void changeFragment(Fragment from, Fragment to) {

        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (!to.isAdded()) {
            if (from != null) {
                fragmentTransaction.hide(from);
            }
            fragmentTransaction.add(binding.naviFrame.getId(), to).commit();
        } else {
            // 如果to已经被添加，可以选择更新它或者什么也不做
            // 例如，可以重新附加它或者调用它的某个方法来更新内容
            // 这里我们简单地调用show方法，如果之前隐藏了的话
            if (from != null) {
                fragmentTransaction.hide(from);
            }
            fragmentTransaction.show(to).commit();
        }
        current = to;

    }

}